package com.zx.Servlet;

import com.zx.Service.UserService;
import com.zx.Service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * com.zx.Servlet
 * 26832
 * zx
 * &#064;description:
 * &#064;author: lxm
 * LoginServlet
 * &#064;create: 2024-06-21 16:39
 **/
@WebServlet("/loginservlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        System.out.println("phone:"+phone+" password:"+password);
        //调用登录业务
        int x = -1;
        UserService userService = new UserServiceImpl();
        try {
             x = userService.login(phone, password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        System.out.println(x);
        switch (x){
            case 0:
                resp.getWriter().write("0");
                break;
            case 1:
                resp.getWriter().write("1");
                break;
            case 2:
                resp.getWriter().write("2");
                break;
            case -1:
                resp.getWriter().write("-1");
                break;
        }
    }
}
